import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    leftmenu: sessionStorage.getItem('leftmenu')
      ? JSON.parse(sessionStorage.getItem('leftmenu'))
      : [],
    token: sessionStorage.getItem('token') || '',
    auth: sessionStorage.getItem('auth')
      ? JSON.parse(sessionStorage.getItem('auth'))
      : [],
    obj: sessionStorage.getItem('obj')
      ? JSON.parse(sessionStorage.getItem('obj'))
      : [],
  },
  mutations: {
    setLeftmenu(state, leftmenu) {
      // 存到sessionstorage
      sessionStorage.setItem('leftmenu', JSON.stringify(leftmenu))
      state.leftmenu = leftmenu
    },
    setToken(state, token) {
      sessionStorage.setItem('token', token)
      state.token = token
    },
    setAuth(state, auth) {
      sessionStorage.setItem('auth', JSON.stringify(auth))
      state.auth = auth
    },
    exit(state) {
      sessionStorage.clear()
      state.token = ''
      state.leftmenu = []
      state.auth = []
    },
    del(state, obj) {
      sessionStorage.setItem('obj', JSON.stringify(obj))
      state.obj = obj
    },
  },
  actions: {},
  modules: {},
})
